package com.stylefeng.guns.modular.system.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.InitCarNumberID;
import com.stylefeng.guns.core.util.KeyUtil;
import com.stylefeng.guns.modular.car.service.ICarService;
import com.stylefeng.guns.modular.system.dao.CarryBasicInformationMapper;
import com.stylefeng.guns.modular.system.model.Car;
import com.stylefeng.guns.modular.system.model.CarryBasicInformation;
import com.stylefeng.guns.modular.system.model.CarryContractInformation;
import com.stylefeng.guns.modular.system.model.Company;
import com.stylefeng.guns.modular.system.model.InitId;
import com.stylefeng.guns.modular.system.model.ModelCarry;
import com.stylefeng.guns.modular.system.model.ModelCarryPrint;
import com.stylefeng.guns.modular.system.model.ModelCarryPrintItem;
import com.stylefeng.guns.modular.system.model.ModelCheck;
import com.stylefeng.guns.modular.system.model.ReceiveBasicInformation;
import com.stylefeng.guns.modular.system.model.ReceiveGoodsInformation;
import com.stylefeng.guns.modular.system.model.ReceiveGoodsStock;
import com.stylefeng.guns.modular.system.model.Route;
import com.stylefeng.guns.modular.system.model.VOCarry;
import com.stylefeng.guns.modular.system.service.ICarryBasicInformationService;
import com.stylefeng.guns.modular.system.service.ICarryContractInformationService;
import com.stylefeng.guns.modular.system.service.ICompanyService;
import com.stylefeng.guns.modular.system.service.IInitIdService;
import com.stylefeng.guns.modular.system.service.IReceiveBasicInformationService;
import com.stylefeng.guns.modular.system.service.IReceiveGoodsInformationService;
import com.stylefeng.guns.modular.system.service.IReceiveGoodsStockService;
import com.stylefeng.guns.modular.system.service.IRouteService;

/**
 * 装车登记控制器
 *
 * @author fengshuonan
 * @Date 2018-04-11 11:54:38
 */
@Controller
@RequestMapping("/carryBasicInformation")
public class CarryBasicInformationController extends BaseController {

    private String PREFIX = "/system/carryBasicInformation/";
    
    
    //为避免号码滥用，失效的号码进行回收
    private String carryId;
    private String carNumberId;
    

    @Autowired
    private ICarryBasicInformationService carryBasicInformationService;
    @Autowired
    private IReceiveBasicInformationService basicInformationService;
    @Autowired
    private IReceiveGoodsInformationService goodsInformationService;
    @Autowired
    private IReceiveGoodsStockService goodsStockService;
    @Autowired
    private ICarService carService;
    @Autowired
    private IRouteService routeService;
    @Autowired
    private ICarryContractInformationService carryContractInformationService;
    @Autowired
    private CarryBasicInformationMapper carryBasicInformationMapper;
    @Autowired
    private IReceiveGoodsStockService receiveGoodsStockService;
    @Autowired
    private ICompanyService companyService;
    
  //装车号生成
    @Autowired
    private IInitIdService initIdService;
    
    public  String init3ID(int id){
		String number = id+"";
		int size = 3-number.length();
		StringBuffer str = new StringBuffer();
		for(int i=0;i<size;i++){
			str.append("0");
		}
		str.append(number);
		return  str.toString();
	}
    
    private String getCarryNumber(){
    	
    	Date now = new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		String nowDate = format.format(now);
		InitId initId  = initIdService.selectList(null).get(0);
		String dbNumber = initId.getCarryId();
		String dbID = dbNumber.substring(8);
		String dbDate = dbNumber.substring(0, 8);
		if(nowDate.equals(dbDate)){
			int ID = Integer.parseInt(dbID);
			ID++;
			return nowDate+init3ID(ID);
		}else{
			return nowDate+"001";
		}
		
    }

    /**
     * 跳转到装车登记首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "carryBasicInformation.html";
    }
    

    /**
     * 审核跳转
     */
    @RequestMapping("check")
    public String index02() {
    	return "/check/carryBasicInformation/" + "carryBasicInformation.html";
    }
    
    /**
     * 查询跳转
     */
    @RequestMapping("boss")
    public String index03() {
    	return "/query/carryBasicInformation/" + "carryBasicInformation.html";
    }
    
    /**
     * 跳转到添加装车登记
     */
    @RequestMapping("/carryBasicInformation_add")
    public String carryBasicInformationAdd(Model model) {
    	if(carNumberId==null||carNumberId==""){
    		carNumberId = InitCarNumberID.getInstance(carryBasicInformationService).initDisorderID();
    	}
    	String carryId =getCarryNumber();
    	ShiroUser shiroUser = ShiroKit.getUser();
    	String username=shiroUser.getName();
    	model.addAttribute("username", username);
    	model.addAttribute("carryId", carryId);
    	model.addAttribute("carNumberId", carNumberId);
    	
        return PREFIX + "carryBasicInformation_add.html";
    }
    
    /**
     * 通过合同号模糊查询合同信息到前台显示，进行装车操作
     */
    @RequestMapping(value="/getbycontractnumber")
    @ResponseBody
    public Object getByContractNumber(@RequestParam String contractNumber){
    	//初始化辅助视图类
    	VOCarry carry = new VOCarry();
    	List<ReceiveGoodsInformation> receiveGoodsInformations = new ArrayList<ReceiveGoodsInformation>();
    	carry.setReceiveGoodsInformations(receiveGoodsInformations);
    	
    	EntityWrapper<ReceiveBasicInformation> entityWrapper = new EntityWrapper<>();
    	Wrapper<ReceiveBasicInformation> wrapper = entityWrapper.eq("receive_contract_number", contractNumber);
    	List<ReceiveBasicInformation> receiveBasicInformations = basicInformationService.selectList(wrapper);
    	ReceiveBasicInformation receiveBasicInformation = receiveBasicInformations.get(0);
    	carry.setReceiveBasicInformation(receiveBasicInformation);
    	
		EntityWrapper<ReceiveGoodsInformation> entityWrapper1 = new EntityWrapper<>();
    	Wrapper<ReceiveGoodsInformation> wrapper1 = entityWrapper1.eq("foreign_key_contract_number", contractNumber);
		List<ReceiveGoodsInformation> basicGoodsInformations = goodsInformationService.selectList(wrapper1);
		for(ReceiveGoodsInformation receiveGoodsInformation:basicGoodsInformations){
			String goodsId = receiveGoodsInformation.getReceiveGoodsId();
			
			EntityWrapper<ReceiveGoodsStock> entityWrapper2 = new EntityWrapper<>();
        	Wrapper<ReceiveGoodsStock> wrapper2 = entityWrapper2.eq("foreign_key_receive_goods_id", goodsId).eq("foreign_contract_number", contractNumber);
        	List<ReceiveGoodsStock> ReceiveGoodsStocks = goodsStockService.selectList(wrapper2);
        	receiveGoodsInformation.setReceiveGoodsStock(ReceiveGoodsStocks.get(0));
        	carry.getReceiveGoodsInformations().add(receiveGoodsInformation);
		}
    	return carry;
    }
    
    /**
     * 通过合同号模糊查询合同信息到前台显示，进行装车操作
     */
    @RequestMapping(value="/querybycontractnumber")
    @ResponseBody
    public Object queryByContractNumber(@RequestParam String searchContract){
    	if(searchContract==null||searchContract==""){
    		searchContract="1";
    	}
    	EntityWrapper<ReceiveBasicInformation> entityWrapper = new EntityWrapper<>();
    	//选取状态为未完成或未开始的合同
    	Wrapper<ReceiveBasicInformation> wrapper;
    	wrapper = entityWrapper.eq("receive_isfinish", KeyUtil.FINISH_STATE_UNSTART).or().eq("receive_isfinish", KeyUtil.FINISH_STATE_UNFINISHED);
    		wrapper = entityWrapper.andNew().like("receive_contract_number", searchContract).last("limit 10");
    	List<ReceiveBasicInformation> receiveBasicInformations = basicInformationService.selectList(wrapper);
    	return receiveBasicInformations;
    }
    
    /**
     * 制单员的重新编辑
     */
    @RequestMapping("/carryBasicInformation_update/{carryBasicInformationId}")
    public String carryBasicInformationUpdate(@PathVariable Integer carryBasicInformationId, Model model) {
    	ModelCarry carry = new ModelCarry();
    	
        CarryBasicInformation carryBasicInformation = carryBasicInformationService.selectById(carryBasicInformationId);
        String carryId = carryBasicInformation.getCarryId();
        EntityWrapper<CarryContractInformation> entityWrapper = new EntityWrapper<>();
        Wrapper<CarryContractInformation> wrapper = entityWrapper.eq("foreign_key_carry_id", carryId);
    	List<CarryContractInformation> carryContractInformations = carryContractInformationService.selectList(wrapper);
    	
    	carry.setCarryBasicInformation(carryBasicInformation);
    	carry.setCarryContractInformations(carryContractInformations);
        model.addAttribute("item",carry);
        LogObjectHolder.me().set(carry);
        return PREFIX + "carryBasicInformation_edit.html";
    }

    /**
     * 制单员重新编辑的提交
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@RequestBody ModelCarry modelCarry) {
    	CarryBasicInformation carryBasicInformation = modelCarry.getCarryBasicInformation();
    	//判断是否已经通过审核了，如果通过审核，则更新失败
    	if(carryBasicInformationService.selectById(carryBasicInformation.getId()).getCarryState().equals(KeyUtil.CHECK_STATE_SUCCESS_CHECKED)){
    		return "FAIL";
    	}
    	//判断车号是否是从数据库中获取的
    	if(modelCarry.getCarId().equals("yes")){
    		
    	}else{
    		//从登记表中获取车辆信息并存储
    		Car car = new Car();
    		car.setCarId(modelCarry.getCarryBasicInformation().getCarryCarId());
    		car.setCarDriver(modelCarry.getCarryBasicInformation().getCarryCarDriver());
    		car.setCarDriverTell(modelCarry.getCarryBasicInformation().getCarryCarDriverTell());
    		car.setCarDriverMobile(modelCarry.getCarryBasicInformation().getCarryCarDriverMobile());
    		carService.insert(car);
    	}
    	
    	//判断线路信息是否是从数据库中获取的
    	if(modelCarry.getRouteId().equals("yes")){
    		
    	}else{
    		Route route = new Route();
    		route.setRouteId(modelCarry.getCarryBasicInformation().getCarryRouteId());
    		route.setRouteStart(modelCarry.getCarryBasicInformation().getCarryRouteStart());
    		route.setRouteEnd(modelCarry.getCarryBasicInformation().getCarryRouteEnd());
    		routeService.insert(route);
    	}
    	
    	 //更新装车基本信息表
        carryBasicInformation.setCarryState(KeyUtil.CHECK_STATE_UNCHECKED);
    	carryBasicInformationService.updateAllColumnById(carryBasicInformation);
    	
    	//存储装车详情合同物品表
    	List<CarryContractInformation> carryContractInformations = modelCarry.getCarryContractInformations();
        for(CarryContractInformation carryContractInformation:carryContractInformations){
        	//如果该装车合同物品id为空，则是新增的信息，需要插入到数据库中
        	if(carryContractInformation.getId()==null||carryContractInformation.getId()==0){
        		carryContractInformation.setForeignKeyCarryId(carryBasicInformation.getCarryId());
        		carryContractInformationService.insert(carryContractInformation);
        	}else{
        		carryContractInformationService.updateAllColumnById(carryContractInformation);
        	}
        }
        return "SUCCESS";
    }

    
    /**
     * 获取装车登记列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<CarryBasicInformation> page = new PageFactory<CarryBasicInformation>().defaultPage();
    	EntityWrapper<CarryBasicInformation> entityWrapper = new EntityWrapper<>();
    	ShiroUser shiroUser = ShiroKit.getUser();
    	String username=shiroUser.getName();
    	//倒序排序
    	List<String> str= new ArrayList<>();
		str.add("id");
		entityWrapper.eq("carry_state", KeyUtil.CHECK_STATE_UNCHECKED).or().eq("carry_state", KeyUtil.CHECK_STATE_FAIL_UNCHECKED);
    	Wrapper<CarryBasicInformation> wrapper = entityWrapper.andNew().eq("carry__trade_person", username).orderDesc(str);
        page.setRecords(carryBasicInformationMapper.selectPage(page,wrapper));
		return super.packForBT(page); 
    }

    /**
     * 新增装车登记
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody ModelCarry modelCarry) {
    	//判断车号是否是从数据库中获取的
    	if(modelCarry.getCarId().equals("yes")){
    		
    	}else{
    		//从登记表中获取车辆信息并存储
    		Car car = new Car();
    		car.setCarId(modelCarry.getCarryBasicInformation().getCarryCarId());
    		car.setCarDriver(modelCarry.getCarryBasicInformation().getCarryCarDriver());
    		car.setCarDriverTell(modelCarry.getCarryBasicInformation().getCarryCarDriverTell());
    		car.setCarDriverMobile(modelCarry.getCarryBasicInformation().getCarryCarDriverMobile());
    		carService.insert(car);
    	}
    	
    	//判断线路信息是否是从数据库中获取的
    	if(modelCarry.getRouteId().equals("yes")){
    		
    	}else{
    		Route route = new Route();
    		route.setRouteId(modelCarry.getCarryBasicInformation().getCarryRouteId());
    		route.setRouteStart(modelCarry.getCarryBasicInformation().getCarryRouteStart());
    		route.setRouteEnd(modelCarry.getCarryBasicInformation().getCarryRouteEnd());
    		routeService.insert(route);
    	}
    	
    	
    	 //存储装车基本信息表
        CarryBasicInformation carryBasicInformation = modelCarry.getCarryBasicInformation();
        carryBasicInformation.setCarryState(KeyUtil.CHECK_STATE_UNCHECKED);
    	carryBasicInformationService.insert(carryBasicInformation);
    	
    	//存储装车详情合同物品表
    	List<CarryContractInformation> carryContractInformations = modelCarry.getCarryContractInformations();
        for(CarryContractInformation carryContractInformation:carryContractInformations){
        	carryContractInformation.setForeignKeyCarryId(carryBasicInformation.getCarryId());
        	carryContractInformationService.insert(carryContractInformation);
        }
        
        //增加成功后，使用掉一个号码
        carNumberId="";
        
        InitId initIdDB  = initIdService.selectList(null).get(0);
		long dbNumber = Long.parseLong(initIdDB.getCarryId().trim());
		long number = Long.parseLong(carryBasicInformation.getCarryId().trim());
		if(number-dbNumber>0){
			//增加成功后，使用掉一个号码
			InitId initId = new InitId();
			initId.setId(1);
			initId.setCarryId(carryBasicInformation.getCarryId());
			initIdService.updateById(initId);
		}
        return "SUCCESS";
    }

    /**
     * 删除装车登记
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer carryBasicInformationId) {
    	CarryBasicInformation carryBasicInformation = carryBasicInformationService.selectById(carryBasicInformationId);
    	
        EntityWrapper<CarryContractInformation> entityWrapper = new EntityWrapper<>();
        Wrapper<CarryContractInformation> wrapper = entityWrapper.eq("foreign_key_carry_id", carryBasicInformation.getCarryId());
        carryBasicInformationService.deleteById(carryBasicInformationId);
        carryContractInformationService.delete(wrapper);
        return SUCCESS_TIP;
    }
    
    /**
     * 修改时点击删除按钮，将信息从数据库中删除
     */
    @RequestMapping(value = "/deleteedit")
    @ResponseBody
    public Object deleteEdit(Integer id){
    	CarryContractInformation carryContractInformation = carryContractInformationService.selectById(id);
    	 EntityWrapper<CarryBasicInformation> entityWrapper = new EntityWrapper<>();
         Wrapper<CarryBasicInformation> wrapper = entityWrapper.eq("carry_id", carryContractInformation.getForeignKeyCarryId());
         CarryBasicInformation carryBasicInformation = carryBasicInformationService.selectList(wrapper).get(0);
         if(carryBasicInformation.getCarryState().equals(KeyUtil.CHECK_STATE_SUCCESS_CHECKED)){
        	 return "FAIL";
         }
    	carryContractInformationService.deleteById(id);
    	return "SUCCESS";
    }

    
    /**
     * 装车登记详情
     */
    @RequestMapping(value = "/detail/{carryBasicInformationId}")
    @ResponseBody
    public Object detail(@PathVariable("carryBasicInformationId") Integer carryBasicInformationId) {
        return carryBasicInformationService.selectById(carryBasicInformationId);
    }
    
    /**
     * 获取装车登记,此用于审核员审核的“未审核”的表数据
     */
    @RequestMapping(value = "/listcheck")
    @ResponseBody
    public Object listCheck(){
    	Page<CarryBasicInformation> page = new PageFactory<CarryBasicInformation>().defaultPage();
    	EntityWrapper<CarryBasicInformation> entityWrapper = new EntityWrapper<>();
    	//倒序排序
    	List<String> str= new ArrayList<>();
    	str.add("id");
    	Wrapper<CarryBasicInformation> wrapper = entityWrapper.eq("carry_state", KeyUtil.CHECK_STATE_UNCHECKED).orderDesc(str);
    	
		page.setRecords(carryBasicInformationMapper.selectPage(page,wrapper));
		return super.packForBT(page); 
    }
    
    /**
     * 打开审核员审核的详情页面
     */
    @RequestMapping("/carryBasicInformation_updatecheck/{carryBasicInformationId}")
    public String receiveBasicInformationUpdateQuery(@PathVariable Integer carryBasicInformationId, Model model) {
    	CarryBasicInformation carryBasicInformation = carryBasicInformationService.selectById(carryBasicInformationId);
    	
        EntityWrapper<CarryContractInformation> entityWrapper = new EntityWrapper<>();
        Wrapper<CarryContractInformation> wrapper = entityWrapper.eq("foreign_key_carry_id", carryBasicInformation.getCarryId());
        List<CarryContractInformation> carryContractInformations = carryContractInformationService.selectList(wrapper);
        
        ModelCarry modelCarry = new ModelCarry();
        modelCarry.setCarryBasicInformation(carryBasicInformation);
        modelCarry.setCarryContractInformations(carryContractInformations);
      //获取审核员信息
    	ShiroUser shiroUser = ShiroKit.getUser();
    	String username=shiroUser.getName();
    	 model.addAttribute("username",username);
        model.addAttribute("item",modelCarry);
        LogObjectHolder.me().set(modelCarry);
        return "/check/carryBasicInformation/" + "carryBasicInformation_edit.html";
    }
    
    /**
     * 审核员判定是否审核通过
     */
    @RequestMapping(value = "/updatecheck")
    @ResponseBody
    public Object updateCheck(@RequestBody ModelCheck modelCheck) {
    	int id = modelCheck.getId();
    	String type = modelCheck.getType();
    	CarryBasicInformation carryBasicInformation = carryBasicInformationService.selectById(id);
    	
    	//判断该记录是否是未审核状态
    	if(!carryBasicInformation.getCarryState().equals(KeyUtil.CHECK_STATE_UNCHECKED)){
    		return "FAIL";
    	}
    	
    	//判断是否通过审核
    	if(type.equals("yes")){
    		//获取该装车登记中所有的货物信息
    		String carryId = carryBasicInformation.getCarryId();
    		EntityWrapper<CarryContractInformation> entityWrapper = new EntityWrapper<>();
        	Wrapper<CarryContractInformation> wrapper = entityWrapper.eq("foreign_key_carry_id", carryId);
    		List<CarryContractInformation> carryContractInformations = carryContractInformationService.selectList(wrapper);
    		
    		//将货物信息更新到库存表中
    		for(CarryContractInformation carryContractInformation:carryContractInformations){
    			String contractNumber = carryContractInformation.getCarryContractNumber();
    			String goodsId = carryContractInformation.getCarryGoodsId();
    			//该货物库存更新
    			int stockCount = carryContractInformation.getCarryUnQuantity();
    			EntityWrapper<ReceiveGoodsStock> entityWrapper1 = new EntityWrapper<>();
    			Wrapper<ReceiveGoodsStock> wrapper1 = entityWrapper1.eq("foreign_contract_number", contractNumber).eq("foreign_key_receive_goods_id", goodsId);
    			ReceiveGoodsStock receiveGoodsStock = receiveGoodsStockService.selectList(wrapper1).get(0);
    			receiveGoodsStock.setReceiveGoodsQuantity(stockCount);
    			receiveGoodsStockService.updateAllColumnById(receiveGoodsStock);
    			
    			//将货物所处的合同状态置为未完成状态
    			EntityWrapper<ReceiveBasicInformation> entityWrapper2 = new EntityWrapper<>();
    			Wrapper<ReceiveBasicInformation> wrapper2 = entityWrapper2.eq("receive_contract_number", contractNumber);
    			ReceiveBasicInformation receiveBasicInformation = basicInformationService.selectList(wrapper2).get(0);
    			receiveBasicInformation.setReceiveIsfinish(KeyUtil.FINISH_STATE_UNFINISHED);
    			basicInformationService.updateAllColumnById(receiveBasicInformation);
    			//如果库存为0的话，判断该合同下其他货物数量是否为0，如果都为0，则合同状态置为已完成
    			if(stockCount==0){
    				EntityWrapper<ReceiveGoodsStock> entityWrapper3 = new EntityWrapper<>();
        			Wrapper<ReceiveGoodsStock> wrapper3 = entityWrapper3.eq("foreign_contract_number", contractNumber);
        			List<ReceiveGoodsStock> receiveGoodsStocks = receiveGoodsStockService.selectList(wrapper3);
        			boolean isFinish = true;
        			for(ReceiveGoodsStock receiveGoodsStock1:receiveGoodsStocks){
        				if(receiveGoodsStock1.getReceiveGoodsQuantity()!=0){
        					isFinish = false;
        					break;
        				}
        			}
        			if(isFinish){
        				receiveBasicInformation.setReceiveIsfinish(KeyUtil.FINISH_STATE_FINISH);
        				basicInformationService.updateAllColumnById(receiveBasicInformation);
        			}
    			}
    		}
    		//获取审核员信息
        	ShiroUser shiroUser = ShiroKit.getUser();
        	String username=shiroUser.getName();
        	carryBasicInformation.setCarryCheck(username);
    		//将审核信息置为已审核
    		carryBasicInformation.setCarryState(KeyUtil.CHECK_STATE_SUCCESS_CHECKED);
    		carryBasicInformationService.updateAllColumnById(carryBasicInformation);
    		return "SUCCESS";
    	}else if(type.equals("no")){
    		carryBasicInformation.setCarryState(KeyUtil.CHECK_STATE_FAIL_UNCHECKED);
    		carryBasicInformationService.updateAllColumnById(carryBasicInformation);
    		return "SUCCESS";
    	}
    	return "FAIL";
    }
    
    /**
     * 获取装车登记列表,此权限用于查询所有已通过审核的功能
     */
    @RequestMapping(value = "/listbossquery")
    @ResponseBody
    public Object list(@RequestParam(required = false) String carryId , @RequestParam(required = false) String carryNumber , @RequestParam(required = false) String carryCarId ,@RequestParam(required = false) String carryCarDriver,
    		@RequestParam(required = false) String carryCarDriverMobile,@RequestParam(required = false) String carryRouteId,@RequestParam(required = false) String carryDate) {
    	Page<CarryBasicInformation> page = new PageFactory<CarryBasicInformation>().defaultPage();
    	List<CarryBasicInformation> result = carryBasicInformationService.getCarryBasic(page, carryId, carryNumber, carryCarId, carryCarDriver, carryCarDriverMobile, carryRouteId, carryDate);
    	page.setRecords(result);
    	return super.packForBT(page);
    }
    
    /**
     * 打开用于查看详情、打印、反审核的页面
     */
    @RequestMapping("/carryBasicInformation_updatequery/{carryBasicInformationId}")
    public String receiveBasicInformationQuery(@PathVariable Integer carryBasicInformationId, Model model) {
    	CarryBasicInformation carryBasicInformation = carryBasicInformationService.selectById(carryBasicInformationId);
    	
        EntityWrapper<CarryContractInformation> entityWrapper = new EntityWrapper<>();
        Wrapper<CarryContractInformation> wrapper = entityWrapper.eq("foreign_key_carry_id", carryBasicInformation.getCarryId());
        List<CarryContractInformation> carryContractInformations = carryContractInformationService.selectList(wrapper);
        
        ModelCarryPrint modelCarryPrint = new ModelCarryPrint();
        modelCarryPrint.setCarryBasicInformation(carryBasicInformation);
        List<ModelCarryPrintItem> modelCarryPrintItems = new ArrayList<>();
        //根据装车货物的合同号寻找到合同号
        for( CarryContractInformation carryContractInformation:carryContractInformations){
        	ModelCarryPrintItem modelCarryPrintItem = new ModelCarryPrintItem();
        	modelCarryPrintItem.setCarryContractInformation(carryContractInformation);
        	String contactNumber = carryContractInformation.getCarryContractNumber();
        	 EntityWrapper<ReceiveBasicInformation> entityWrapper1 = new EntityWrapper<>();
             Wrapper<ReceiveBasicInformation> wrapper1 = entityWrapper1.eq("receive_contract_number", contactNumber);
             ReceiveBasicInformation receiveBasicInformation = basicInformationService.selectList(wrapper1).get(0);
             modelCarryPrintItem.setReceiveBasicInformation(receiveBasicInformation);
             modelCarryPrintItems.add(modelCarryPrintItem);
        }
        modelCarryPrint.setModelCarryPrintItems(modelCarryPrintItems);
        model.addAttribute("item",modelCarryPrint);
        LogObjectHolder.me().set(modelCarryPrint);
        Company company = companyService.selectList(null).get(0);
        model.addAttribute("company",company);
        return "/query/carryBasicInformation/" + "carryBasicInformation_edit.html";
    }
    
    /**
     * 反审核操作
     */
    @RequestMapping(value = "/antiAudit")
    @ResponseBody
    public Object antiAudit(Integer id) {
    	CarryBasicInformation carryBasicInformation = carryBasicInformationService.selectById(id);
    	
    	//获取该装车登记下所有合同物品
    	 EntityWrapper<CarryContractInformation> entityWrapper = new EntityWrapper<>();
         Wrapper<CarryContractInformation> wrapper = entityWrapper.eq("foreign_key_carry_id", carryBasicInformation.getCarryId());
         List<CarryContractInformation> carryContractInformations = carryContractInformationService.selectList(wrapper);
         for(CarryContractInformation carryContractInformation:carryContractInformations){
        	 
        	 //获取该合同物品下的库存信息
        	 int thisQuantity = carryContractInformation.getCarryThisQuantity();
        	 EntityWrapper<ReceiveGoodsStock> entityWrapper1 = new EntityWrapper<>();
             Wrapper<ReceiveGoodsStock> wrapper1 = entityWrapper1.eq("foreign_contract_number", carryContractInformation.getCarryContractNumber()).eq("foreign_key_receive_goods_id", carryContractInformation.getCarryGoodsId());
             ReceiveGoodsStock receiveGoodsStock  = receiveGoodsStockService.selectList(wrapper1).get(0);
             //还原库存 更新
             int stockCount = receiveGoodsStock.getReceiveGoodsQuantity();
             int sum = stockCount+thisQuantity;
             receiveGoodsStock.setReceiveGoodsQuantity(sum);
             receiveGoodsStockService.updateAllColumnById(receiveGoodsStock);
             //该合同状态置为未完成
             EntityWrapper<ReceiveBasicInformation> entityWrapper2 = new EntityWrapper<>();
             Wrapper<ReceiveBasicInformation> wrapper2 = entityWrapper2.eq("receive_contract_number", carryContractInformation.getCarryContractNumber());
             ReceiveBasicInformation receiveBasicInformation = basicInformationService.selectList(wrapper2).get(0);
             receiveBasicInformation.setReceiveIsfinish(KeyUtil.FINISH_STATE_UNFINISHED);
             basicInformationService.updateAllColumnById(receiveBasicInformation);
             
             
             //如果一个货物更新后的库存等于最开始发货数量时,计算该合同下其他货物的库存是否也等于最开始发货数量
             if(sum==carryContractInformation.getCarryGoodsQuantity()){
            	 
            	 EntityWrapper<ReceiveGoodsStock> entityWrapper3 = new EntityWrapper<>();
                 Wrapper<ReceiveGoodsStock> wrapper3 = entityWrapper3.eq("foreign_contract_number", carryContractInformation.getCarryContractNumber());
                 List<ReceiveGoodsStock> receiveGoodsStocks = receiveGoodsStockService.selectList(wrapper3);
                 
                 boolean equals = true;
                 
                 //对比每一个库存数量
                 for(ReceiveGoodsStock receiveGoodsStock1:receiveGoodsStocks){
                	 EntityWrapper<ReceiveGoodsInformation> entityWrapper4 = new EntityWrapper<>();
                     Wrapper<ReceiveGoodsInformation> wrapper4 = entityWrapper4.eq("foreign_key_contract_number", receiveGoodsStock1.getForeignContractNumber()).eq("receive_goods_id", receiveGoodsStock1.getForeignKeyReceiveGoodsId());
                     ReceiveGoodsInformation receiveGoodsInformation2 = goodsInformationService.selectList(wrapper4).get(0);
                     //如果库存与总数量不相等
                     if(receiveGoodsStock1.getReceiveGoodsQuantity()!=new Double(receiveGoodsInformation2.getReceiveGoodsQuantity()).intValue()){
                    	 equals = false;
                    	 break;
                     }
                 }
                 //如果全部相等,合同置为未开始
                 if(equals){
                	 receiveBasicInformation.setReceiveIsfinish(KeyUtil.FINISH_STATE_UNSTART);
                     basicInformationService.updateAllColumnById(receiveBasicInformation);
                 }
             }
         }
         //将该装车登记置为未审核
         carryBasicInformation.setCarryState(KeyUtil.CHECK_STATE_UNCHECKED);
         //审核人员清空
         carryBasicInformation.setCarryCheck(null);
         carryBasicInformationService.updateAllColumnById(carryBasicInformation);
         return "SUCCESS";
    }
    
 
}
